package com.foreknow.elm.controller;

import com.foreknow.elm.po.Cart;
import com.foreknow.elm.service.CartService;
import com.foreknow.elm.service.impl.CartServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author shkstart
 * @create 2021-08-26 17:19
 */
public class CartController {
    public List<Object> listCart(HttpServletRequest request) throws Exception{
        //获取请求参数
        String userId=null;
        int businessId=0;
        if(request.getParameter("businessId")!=null) {
            businessId=Integer.parseInt(request.getParameter("businessId"));
        }
        if(request.getParameter("userId")!=null) {

            userId=request.getParameter("userId");
        }
        //调用service方法
        CartService service=new CartServiceImpl();
        List<Object> list = service.listCart(userId, businessId);




        return list;
    }


    public int saveCart(HttpServletRequest request)throws Exception{
        //获取请求参数
        String userId=request.getParameter("userId");
        int businessId=Integer.parseInt(request.getParameter("businessId"));
        int foodId=Integer.parseInt(request.getParameter("foodId"));
        //调用service
        CartService service=new CartServiceImpl();
        int i = service.saveCart(userId, businessId, foodId);
        return i;
    }

    /**
     * 功能：根据用户编号、商家编号、食品编号更新数量
     * @param request
     * @return
     * @throws Exception
     */
    public int updateCart(HttpServletRequest request)throws Exception{
        //获取请求参数
        String userId=request.getParameter("userId");
        int businessId=Integer.parseInt(request.getParameter("businessId"));
        int foodId=Integer.parseInt(request.getParameter("foodId"));
        int quantity=Integer.parseInt(request.getParameter("quantity"));

        //调用service 方法
        CartService service=new CartServiceImpl();
        int i = service.updateCart(userId, businessId, foodId, quantity);

        return i;


    }

    /**
     * 根据用户编号、商家编号、食品编号删除购物车表中的一条食品记录
     *              根据用户编号、商家编号删除购物车表中的多条条记录
     * @param request
     * @return
     * @throws Exception
     */
    public int removeCart(HttpServletRequest request)throws Exception{
        Cart cart=new Cart();
        String userId="0";
        int businessId=0;
        int foodId=0;
        //获取请求参数
        if(request.getParameter("userId")!=null){
         userId=request.getParameter("userId");
            cart.setUserId(userId);
        }if(request.getParameter("businessId")!=null){
            businessId=Integer.parseInt(request.getParameter("businessId"));
            cart.setBusinessId(businessId);
        }if(request.getParameter("foodId")!=null){

            foodId=Integer.parseInt(request.getParameter("foodId"));
            cart.getFood().setFoodId(foodId);
        }


        CartService service=new CartServiceImpl();
        int i = service.removeCart(cart);
        return i;

    }
}
